package m2;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

public class Consumer {
    public static void main(String[] args) throws MQClientException {
        //创建消费者
        DefaultMQPushConsumer c = new DefaultMQPushConsumer("consumer-m2");
        //设置 name server
        c.setNamesrvAddr("192.168.64.141:9876");
        //从Topic2 订阅 "*" 消息
        c.subscribe("Topic2", "*");
        c.setMessageListener(new MessageListenerOrderly() {
            @Override
            public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext consumeOrderlyContext) {
                for (MessageExt msg : msgs) {
                    String s = new String(msg.getBody());
                    System.out.println("收到："+s);
                }
                //挂起当前队列
                return ConsumeOrderlyStatus.SUCCESS;
            }
        });
        //设置消息监听器，只允许单个线程接收处理消息，不允许多个
        //启动
        c.start();
    }
}
